package org.qs.webservice.controller;


import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.BookRecentService;
import org.qs.webservice.service.BookReviewService;
import org.qs.webservice.service.UserConsumeService;
import org.qs.webservice.service.UserService;
import org.qs.webservice.util.IReadUtils;
import org.qs.webservice.util.StringUtils;
import org.springframework.stereotype.Component; 


/**
 * 获取某个区块中素材列表接口
 * @ClassName: BlockRestController 
 * @author xusaike 
 * @date 2015年9月28日 下午2:39:54 
 *
 */
@Component
@Path("/v{version}/me")
public class MyInfoRestController {

	@Resource
	private UserService userService;
	@Resource
	private UserConsumeService userConsumeService;
	@Resource
	private BookRecentService bookRecentService;
	@Resource
	private BookReviewService bookReviewService;

	@GET  
	@Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)  
	public String getMe(@QueryParam("token")  String token,@HeaderParam("Info-Uuid")String device_identifier,
			@QueryParam("device_identifier")String device_identifier_Q,@HeaderParam("token")String token_str){  
		try{
			device_identifier=IReadUtils.preventInjection(device_identifier);
			device_identifier_Q=IReadUtils.preventInjection(device_identifier_Q);
			if(!StringUtils.isNullOrEmpty(device_identifier_Q)){
				device_identifier = device_identifier_Q;
			}
			if(StringUtils.isNullOrEmpty(token)){
				token = token_str;
				//token=URLDecoder.decode(token,"UTF-8");
			}else{
				token=URLEncoder.encode(token,"UTF-8");
			}
			if(StringUtils.isNullOrEmpty(device_identifier)){
				device_identifier = "";
			}
			String str= userService.getMe(token,device_identifier);  
			return str;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "获取个人信息异常！",errorJson);
		}
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		tokenMap.put("code", 403);
		tokenMap.put("reason","服务器错误");
		return IReadUtils.beanToJson(tokenMap);
	}  
	/**
	 * 获取消费列表
	 * @param token
	 * @param device_identifier
	 * @param device_identifier_Q
	 * @param token_str
	 * @return
	 */
	@POST
	@Path("consume")
	@Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)  
	public String getConsume(@FormParam("content_id")  int content_id,
			@FormParam("consume_type")  int consume_type,
			@FormParam("pages")  int pages,
			@FormParam("contents")  int contents,
			@FormParam("begin_time")  String begin_time,
			@FormParam("end_time")  String end_time,
			@HeaderParam("Info-Uuid")String device_identifier,
			@HeaderParam("token")String token){  
		try{
			device_identifier=IReadUtils.preventInjection(device_identifier);
			if(StringUtils.isNullOrEmpty(device_identifier)){
				device_identifier = "";
			}
			String str= userConsumeService.getConsume(content_id,consume_type,token,device_identifier,pages,contents,begin_time,end_time);  
			return str;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "获取个人信息异常！",errorJson);
		}
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		tokenMap.put("code", 403);
		tokenMap.put("reason","服务器错误");
		return IReadUtils.beanToJson(tokenMap);
	}  
	@POST
	@Path("recent")
	@Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)  
	public String getRecents(
			@FormParam("pages")  int pages,
			@FormParam("contents")  int contents,
			@FormParam("order")  String order,
			@HeaderParam("Info-Uuid")String device_identifier,
			@HeaderParam("token")String token){  
		try{
			device_identifier=IReadUtils.preventInjection(device_identifier);
			if(StringUtils.isNullOrEmpty(device_identifier)){
				device_identifier = "";
			}
			String str= bookRecentService.getRecents(token,device_identifier,pages,contents, order);  
			return str;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "获取个人信息异常！",errorJson);
		}
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		tokenMap.put("code", 403);
		tokenMap.put("reason","服务器错误");
		return IReadUtils.beanToJson(tokenMap);
	}  
	@POST
	@Path("review")
	@Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)  
	public String getReview(
			@FormParam("pages")  int pages,
			@FormParam("contents")  int contents,
			@HeaderParam("Info-Uuid")String device_identifier,
			@HeaderParam("token")String token){  
		try{
			device_identifier=IReadUtils.preventInjection(device_identifier);
			if(StringUtils.isNullOrEmpty(device_identifier)){
				device_identifier = "";
			}
			String str= bookReviewService.getReviewListByUser(pages, contents, "desc", device_identifier,token);
			return str;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "获取个人信息异常！",errorJson);
		}
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		tokenMap.put("code", 403);
		tokenMap.put("reason","服务器错误");
		return IReadUtils.beanToJson(tokenMap);
	}  
	@POST
	@Path("recharge")
	@Produces(javax.ws.rs.core.MediaType.TEXT_PLAIN)  
	public String getRecharge(
			@FormParam("pages")  int pages,
			@FormParam("type")  int type,
			@FormParam("contents")  int contents,
			@HeaderParam("Info-Uuid")String device_identifier,
			@HeaderParam("token")String token){  
		try{
			device_identifier=IReadUtils.preventInjection(device_identifier);
			if(StringUtils.isNullOrEmpty(device_identifier)){
				device_identifier = "";
			}
			String str= userService.getRecharge(type,pages,contents,device_identifier,token);
			return str;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("token", token);
			errorJson.put("device_identifier", device_identifier);
			QSReadException.printf(e, "获取个人信息异常！",errorJson);
		}
		Map<String,Object> tokenMap = new HashMap<String,Object>();
		tokenMap.put("code", 403);
		tokenMap.put("reason","服务器错误");
		return IReadUtils.beanToJson(tokenMap);
	}  
}